﻿using Abp;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Events.Bus.Entities;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Runtime.Session;
using Abp.Threading.BackgroundWorkers;
using Abp.Timing;
using Abp.Zero;
using Abp.Zero.Configuration;
using AbpProjectTemplate.Components.Auditing;
using AbpProjectTemplate.Components.Authorization;
using AbpProjectTemplate.Components.Configuration;
using AbpProjectTemplate.Components.DynamicEntityProperties;
using AbpProjectTemplate.Components.Events.Entities;
using AbpProjectTemplate.Components.MultiTenancy;
using AbpProjectTemplate.Components.Payments;
using AbpProjectTemplate.Components.Runtime;
using AbpProjectTemplate.Domain.Roles;
using AbpProjectTemplate.Domain.Users;
using AbpProjectTemplate.Localization;
using AbpProjectTemplate.Utilities.Enums;
using AbpProjectTemplate.Workers;

namespace AbpProjectTemplate
{
    [DependsOn(typeof(AbpZeroCoreModule))]
    public class AppCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            // Enable this line to create a multi-tenant application.
            this.Configuration.MultiTenancy.IsEnabled = AppConsts.MultiTenancyEnabled;

            //审计配置
            this.Configuration.Auditing.IsEnabled = true;
            this.Configuration.Auditing.Selectors.Clear();

            // 多语言配置
            this.Configuration.Localization.IsEnabled = AppConsts.LocalizationEnabled;
            if (this.Configuration.Localization.IsEnabled)
                this.Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
            this.Configuration.Localization.HumanizeTextIfNotFound = false;
            this.Configuration.Localization.ReturnGivenTextIfNotFound = true;
            this.Configuration.Localization.WrapGivenTextIfNotFound = false;
            this.Configuration.Localization.AddXmlSource(this, AppConsts.LocalizationSourceName);
            this.Configuration.Localization.ExtendXmlSource(this, AbpConsts.LocalizationSourceName);
            this.Configuration.Localization.ExtendXmlSource(this, AbpZeroConsts.LocalizationSourceName);

            // Declare entity types
            this.Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            this.Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            this.Configuration.Modules.Zero().EntityTypes.User = typeof(User);
            //角色初始化
            this.Configuration.Modules.Zero().RoleManagement.Init();
            //自动配置添加
            this.Configuration.AddAutoSettings();

            //服务替换
            this.IocManager.Register<IEntityChangeEventHelper, AppEntityChangeEventHelper>();
            this.IocManager.Register<IAbpSession, AppSession>();
            this.IocManager.Register<IPermissionChecker, AppPermissionChecker>();
            this.IocManager.Register<IAuditingHelper, AppAuditingHelper>();
            this.IocManager.Register<IAuditingStore, AppAuditingStore>();
            //注册支付
            this.IocManager.RegisterPayment();
            //注册枚举
            this.IocManager.RegisiterEnum();


            this.Configuration.DynamicEntityProperties.Providers.Add<AppDynamicEntityPropertyDefinitionProvider>();
        }

        public override void Initialize()
        {
            this.IocManager.RegisterAssemblyByConvention(typeof(AppCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            this.IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;

            //自动注册后台工作者
            var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
            var workers = IocManager.ResolveAll<IAppWorker>();
            foreach (var worker in workers)
                workManager.Add(worker);
        }
    }
}

